/**
 * @author ciwyaitd
 * @date   15/11/19
 * @description []
 */
import gulp from 'gulp'
import babel from 'gulp-babel'
import browserSync from 'browser-sync'
import changed from 'gulp-changed'
import clean from 'gulp-clean'
import gulpif from 'gulp-if'
import plumber from 'gulp-plumber'
import replace from 'gulp-replace'
import rev from 'gulp-rev'
import revCollector from 'gulp-rev-collector'
import sass from 'gulp-sass'
import sequence from 'gulp-sequence'
import sourcemaps from 'gulp-sourcemaps'
import uglify from 'gulp-uglify'
import swig from 'gulp-swig'
import data from 'gulp-data'
import cached from 'gulp-cached'
import concat from 'gulp-concat'
let reload = browserSync.reload;

/* ------------- file path ------------- */
let project_name = 'Jise',
    config_dir = './dist/' + project_name + '/',
	filePath = {
		dist: config_dir,
		sass: ['./src/sass/*.scss', './src/sass/*/*.scss'],
		js: ['./lib/*.js','./src/js/*.js'],
        img: './images/**/**.*',
        swig: './swig/*.html',
        swigTpm: './swig/common/*.html',
		dist_css: config_dir + 'src/css',
		dist_js: config_dir + 'src/js',
        dist_html: config_dir + 'html',
        dist_img: config_dir + 'images',
		dist_rev: config_dir + 'rev',
        dist_swig: config_dir + 'html',
        tmp: './.tmp/'+ project_name + '/',
        html: './html/*.html',
        rev_json: config_dir + 'rev/**/*.json'
	},
    watchPath = {
        sass: 'src/sass/**/*.scss',
        js: 'src/js/*.js',
        img: 'images/**/**.*',
        swig: 'swig/**/*.html',
    };

/* ------------- clean ------------- */
gulp.task('clean', () => {
	return gulp.src(filePath.dist, {read: false}).pipe(clean());
});

/* ------------- backup ------------- */

// 不做js压缩,只是为了给js源码.目前不使用,坚决压缩js
gulp.task('distJs', () => {
    return gulp.src(filePath.js)
        .pipe(plumber({
            errorHandler: function () {
                // display the error message
                console.log(err);
                // end the errored task
                this.emit('end')
            }
        }))
        .pipe(changed(filePath.tmp))
        .pipe(gulp.dest(filePath.tmp))
        .pipe(gulpif('!*.min.js', babel()))
        .pipe(gulp.dest(filePath.dist_js));
});

// 压缩和写入映射版本号, 有必要使用版本号时使用
gulp.task('jsUglify', () => {
    return gulp.src(filePath.js)
        .pipe(plumber({
            errorHandler: function () {
                // display the error message
                console.log(err);
                // end the errored task
                this.emit('end')
            }
        }))
        .pipe(changed(filePath.tmp))
        .pipe(gulp.dest(filePath.tmp))
        .pipe(sourcemaps.init())
        .pipe(gulpif('!*.min.js', babel()))
        .pipe(gulpif('!*.min.js', uglify()))
        .pipe(rev())
        .pipe(sourcemaps.write('./maps'))
        .pipe(gulp.dest(filePath.dist_js))
        .pipe(rev.manifest())
        .pipe(gulp.dest(filePath.dist_rev + '/js'));
});

// 添加版本号
gulp.task('addVersion', () => {
    return gulp.src([filePath.rev_json , filePath.html])
        .pipe(revCollector())
        .pipe(gulp.dest(filePath.dist_html));
});

gulp.task('proVersion', ['jsUglify', 'addVersion']);


// 单独压缩js任务
gulp.task('jsConcat', () => {
    return gulp.src(filePath.js[1])
        .pipe(concat('main.min.js'))
    .pipe(plumber({
        errorHandler: function () {
            // display the error message
            console.log(err);
            // end the errored task
            this.emit('end')
        }
    }))
    .pipe(sourcemaps.init())
    .pipe(changed(filePath.tmp))
    .pipe(gulp.dest(filePath.tmp))
    .pipe(uglify())
    .pipe(sourcemaps.write('./maps'))
    .pipe(gulp.dest(filePath.dist_js));
});

/* ------------- build ------------- */
// 编译sass文件
gulp.task('sass', () => {
	return gulp.src(filePath.sass)
        .pipe(plumber({
            errorHandler: function () {
                // display the error message
                console.log(err);
                // end the errored task
                this.emit('end')
            }
        }))
        .pipe(sass({
            outputStyle: 'compressed'
        }))
        .pipe(cached('sass_compile'))
        .pipe(rev())
        .pipe(gulp.dest(filePath.dist_css))
        .pipe(rev.manifest())
        .pipe(gulp.dest(filePath.dist_rev+'/css'));
});

// 监察压缩
gulp.task('jsOnlyUglify', () => {
    return gulp.src(filePath.js)
        .pipe(plumber({
            errorHandler: function () {
                // display the error message
                console.log(err);
                // end the errored task
                this.emit('end')
            }
        }))
        .pipe(changed(filePath.tmp))
        .pipe(gulp.dest(filePath.tmp))
        .pipe(sourcemaps.init())
        .pipe(gulpif('!*.min.js', babel()))
        .pipe(gulpif('!*.min.js', uglify()))
        .pipe(sourcemaps.write('./maps'))
        .pipe(gulp.dest(filePath.dist_js));
});

// 编译swig模板
gulp.task('distSwig', () => {
    return gulp.src(filePath.swig)
    .pipe(changed(filePath.dist_swig, {extension: '.html'}))
    .pipe(swig({defaults: { cache: false }}))
    .pipe(gulp.dest(filePath.dist_swig));
});

gulp.task('distImg', () => {
    gulp.src(filePath.img)
        .pipe(changed(filePath.dist_img))
        .pipe(gulp.dest(filePath.dist_img));
});

gulp.task('distHtml', () => {
    gulp.src(filePath.html)
        .pipe(changed(filePath.dist_html))
        .pipe(gulp.dest(filePath.dist_html));
});

gulp.task('allReload', ['distImg', 'sass', 'jsOnlyUglify', 'distSwig'], () => {
    reload();
});

gulp.task('server', ['distImg', 'sass', 'jsOnlyUglify', 'distSwig'], () => {
    browserSync({
        notify: false,
        port: 2800,
        server: {
            baseDir: filePath.dist,
            directory: true
        }
    });
    gulp.watch(
        [watchPath.swig].concat(watchPath.img, watchPath.sass, watchPath.js)
    , ['allReload']);//.on('change', reload);
});

gulp.task('singleServer', () => {
    browserSync({
        notify: false,
        port: 2800,
        server: {
            baseDir:filePath.dist,
            directory: true
        }
    });
    gulp.watch(
        [watchPath.swig].concat(watchPath.img, watchPath.sass, watchPath.js)
        , ['allReload']);//.on('change', reload);
});



